//
//  UIBarButtonItem+Extension.swift
//  Swift_sina
//
//  Created by TFB_WGP on 2017/8/17.
//  Copyright © 2017年 TFB_WGP. All rights reserved.
//

import Foundation

extension UIBarButtonItem{
    
    //便利构造函数要调用self.init实例化
    convenience init(title: String,fontSize: CGFloat = 16,target: AnyObject?, action: Selector, isBack: Bool) {
        
        let btn: UIButton = UIButton.cz_textButton(title, fontSize: fontSize, normalColor: UIColor.darkGray, color: UIColor.orange, controlState: .highlighted)
        btn.addTarget(target, action: action, for: .touchUpInside)
        if isBack {
            btn.setImage(UIImage(named: "navigationbar_back_withtext"), for: .normal)
            btn.setImage(UIImage(named: "navigationbar_back_withtext_highlighted"), for: .highlighted)
            
            btn.sizeToFit()
        }
        self.init(customView:btn)
    }
    
    convenience init(normalImage: String, highlightImage: String,target: AnyObject?, action: Selector){
        
        let btn = UIButton(type: .custom)
        btn.setBackgroundImage(UIImage(named: normalImage), for: .normal)
        btn.setBackgroundImage(UIImage(named: highlightImage), for: .highlighted)
        btn.sizeToFit()
        
        btn.addTarget(target, action: action, for: .touchUpInside)
        self.init(customView:btn)
    }
    
    convenience  init(title: String, fontSize: CGFloat, normalColor: UIColor, target: Any ,action: Selector) {
        let btn = UIButton(type: .custom)
        btn.setTitle(title, for: .normal)
        btn.setTitleColor(normalColor, for: .normal)
        btn.setTitleColor(normalColor, for: .highlighted)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: fontSize)
        btn.sizeToFit()
        btn.addTarget(target, action: action, for: .touchUpInside)
        self.init(customView:btn)
    }
}
